package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.util.jsoup.HtmlBodyPrase;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;
import com.webull.information.center.common.constants.UrlConstants;
import com.webull.information.client.model.News;

/**
 * 21财经网站获取
 * 
 * @author smj
 * @Date 2017年7月18日下午4:10:03
 * @version 1.0
 *
 */
@Component
public class R1CaiNewsListUitl implements HtmlBodyPrase {
	
	private @Autowired
    HttpJsoupClient htmlPrase;
	protected final Logger logger = LogManager.getLogger(getClass());

	/**
	 * 
	 * @return
	 */
	public List<R1CaiItem> parseNewsList() {

		Document doc = htmlPrase.parseDocumentFromUrl(UrlConstants.R1caiHomeUrl);
		if (doc == null) {
			logger.warn("YiCaiNewsListUitl parseNewsList parse url:{} ,is null!!", UrlConstants.R1caiHomeUrl);
			return null;
		}
		List<R1CaiItem> parseNewsList = parseNewsList(doc);
		List<R1CaiItem> parseNewsList1 = parseNewsList1(doc);

		List<R1CaiItem> result = new ArrayList<>();
		if (parseNewsList != null)
			result.addAll(parseNewsList);
		if (parseNewsList1 != null)
			result.addAll(parseNewsList1);
		return result;
	}

	private List<R1CaiItem> parseNewsList(Document doc) {
		List<R1CaiItem> result = new ArrayList<R1CaiItem>();
		// A股新闻页面
		Optional.ofNullable(doc.select("div#wrapper>ul>li")).ifPresent(dls -> {
			for (Element ele : dls) {
				R1CaiItem opt = new R1CaiItem();
				opt.setHomeType(News.HomeType.frontPage);
				Optional.ofNullable(ele.select("li>a.bannerA>img").first())
						.ifPresent(img0 -> opt.setImg(img0.absUrl("src")));

				Optional.ofNullable(ele.select("li>div.contFont").first()).ifPresent(s0 -> {

					Optional.ofNullable(s0.select("div>a").first()).ifPresent(a0 -> {
						opt.setLink(a0.absUrl("href"));
						opt.setTitle(StringEscapeUtils.unescapeHtml4(a0.text()));
					});

					Optional.ofNullable(s0.select("div>p.cont").first()).ifPresent(p0 -> 
					opt.setSummry(StringEscapeUtils.unescapeHtml4(p0.text())));

					Optional.ofNullable(s0.select("div>p.shuoming").first()).ifPresent(sm0 -> {
						Optional.ofNullable(sm0.select("p>span.time").first()).ifPresent(st0 -> {
							try {
								String source = Optional.ofNullable(st0.nextElementSibling())
										.filter(st1 -> "span".equalsIgnoreCase(st1.tagName()))
										.map(st2 -> StringUtils.stripToNull(st2.text())).orElse(source0);

								opt.setSource(source);
								opt.setNewsTime(new Date(Long.valueOf(st0.attr("data-inputtime") + "000")));
							} catch (Exception e) {
							}
						});
					});

				});

				if (opt.check())
					result.add(opt);
			}

		});
		return result;
	}

	private List<R1CaiItem> parseNewsList1(Document doc) {
		List<R1CaiItem> result = new ArrayList<R1CaiItem>();
		// A股新闻页面
		Optional.ofNullable(doc.select("div#data_list>div.li")).ifPresent(dls -> {
			for (Element ele : dls) {
				R1CaiItem opt = new R1CaiItem();
				opt.setHomeType(News.HomeType.top);
				Optional.ofNullable(ele.select("div>a.listImg>img").first())
						.ifPresent(img0 -> opt.setImg(img0.absUrl("src")));

				Optional.ofNullable(ele.select("div>div.Tlist").first()).ifPresent(s0 -> {

					Optional.ofNullable(s0.select("div>a.listTit").first()).ifPresent(a0 -> {
						opt.setLink(a0.absUrl("href"));
						opt.setTitle(StringEscapeUtils.unescapeHtml4(a0.text()));
					});

					Optional.ofNullable(s0.select("div>p.listCont").first())
							.ifPresent(p0 -> opt.setSummry(StringEscapeUtils.unescapeHtml4(p0.text())));

					Optional.ofNullable(s0.select("div>p.shuoming").first()).ifPresent(sm0 -> {
						Optional.ofNullable(sm0.select("p>span.time").first()).ifPresent(st0 -> {
							try {
								String source = Optional.ofNullable(st0.nextElementSibling())
										.filter(st1 -> "span".equalsIgnoreCase(st1.tagName()))
										.map(st2 -> StringUtils.stripToNull(st2.text())).orElse(source0);

								opt.setSource(source);
								opt.setNewsTime(new Date(Long.valueOf(st0.attr("data-inputtime") + "000")));
							} catch (Exception e) {
							}
						});
					});

				});

				if (opt.check())
					result.add(opt);
			}

		});
		return result;
	}

	private final static String source0 = "21经济网";
	@Override
	public void praseNewsInfo(Document doc, NewsInformation info) {
		try {
			String[] context = { "" };
			praseNewsInfo2(doc, context, 0);

			String context0 = context[0];
			String huanhang = StringUtils.replace("(?i)>{s}<{s}p{s}>", "{s}", "\\s*");
			context0 = StringUtils.replacePattern(context0, huanhang, "><div style='height:19px;'></div><p>");
			info.setContent(context0);
		} catch (Exception e) {
			logger.warn(info.getUrl() + "praseNewsInfo error", e);
		}
	}

	private void praseNewsInfo2(Document doc, String[] context, int loop) {
		if (loop++ >= 4)// 限制最大分页次数
			return;
		if (StringUtils.isBlank(context[0])) {
			Optional.ofNullable(doc.select("p.abstract").first())
					.map(v -> StringUtils.stripToNull(v.text()))
					.ifPresent(v -> context[0] = "<p>摘要 : " + v + "</p>");
		}
		final String regex = "(?i)(http[s]?://)?w{0,3}\\.?21jingji\\.com.?";

		Optional.ofNullable(doc.select("div.detailCont>p")).filter(v -> v.size() > 0).ifPresent(v -> {
			for (int i = 0; i < v.size(); i++) {
				Element element = v.get(i);


				Optional.ofNullable(element.select("p>a")).filter(pa -> pa.size() >= 1).ifPresent(pa -> {
					for (Element a0 : pa) {
						if (a0.absUrl("href").matches(regex))
							a0.remove();
					}
				});

				String text0 = element.text() + "";
				if (i == 1) {
					if (text0.matches("\\s*.{1,20}/文\\s*"))
						element.remove();
				} else if (text0.matches("\\s*.{0,3}作者微信.*")) {
					element.remove();
				} else if (text0.matches("\\s*.{0,3}编辑.*")) {
					element.remove();
				} else if (text0.matches("\\s*.{0,3}编辑.*")) {
					element.remove();
				} else if (StringUtils.equalsIgnoreCase(text0, "<br>")
						|| StringUtils.equalsIgnoreCase(text0, "&nbsp;")) {
					element.remove();
				}

			}

			context[0] += doc.select("div.detailCont>p").outerHtml();
		});

		/*
		 * 处理下一页正文
		 */
		Document document = Optional.ofNullable(doc.select("div#pages>span").first())
				.map(sp0 -> sp0.nextElementSibling())
				.filter(a0 -> "a".equalsIgnoreCase(a0.tagName()) && StringUtils.strip((a0.text() + "")).matches("\\d*")) // 排除下一页那个按钮
				.map(a0 -> a0.absUrl("href"))//
				.map(h0 -> htmlPrase.parseDocumentFromUrl(h0)).orElse(null);
		if (document != null)
			praseNewsInfo2(document, context, loop);
	}

	private static Document get(String url) {
		Connection connection = Jsoup.connect(url).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
		try {
			return connection.timeout(10000).get();
		} catch (IOException e) {
			return null;
		}
	}
	public static void main(String[] args) throws IOException {
 
		NewsInformation newsInformation = new NewsInformation();
		List<R1CaiItem> parseNewsList1 = new R1CaiNewsListUitl()
				.parseNewsList(get("http://www.21jingji.com/channel/money/"));
		System.out.println(parseNewsList1);
	}



	public static class R1CaiItem {
		private Date newsTime;
		private String title;
		private String link;
		private String source;
		private String summry;
		private String img;
		private Integer homeType;

		private boolean check() {
			return newsTime != null && StringUtils.isNotBlank(title) && StringUtils.isNotBlank(link);
		}

		private void setNewsTime(Date newsTime) {
			this.newsTime = newsTime;
		}

		private void setTitle(String title) {
			this.title = title;
		}

		private void setLink(String link) {
			this.link = link;
		}

		private void setSource(String source) {
			this.source = source;
		}

		private void setSummry(String summry) {
			this.summry = summry;
		}

		private void setImg(String img) {
			this.img = img;
		}

		private void setHomeType(Integer homeType) {
			this.homeType = homeType;
		}

		public Date getNewsTime() {
			return newsTime;
		}

		public String getTitle() {
			return title;
		}

		public String getLink() {
			return link;
		}

		public String getSource() {
			return source;
		}

		public String getSummry() {
			return summry;
		}

		public String getImg() {
			return img;
		}

		public Integer getHomeType() {
			return homeType;
		}

	}



}
